// created on 03.08.2007 at 16:18

namespace Gamadus.Engine.Compiler
{
    /// <summary>
    /// A singleton manager. A class can be made a singleton easily 
    /// by inheriting this class here. For example:
    /// <example> This sample shows how to inherit from the singleton
    /// <code>
    /// class Foo : SingletonManager`1Foo { 
    ///     public void Bar() {...}
    /// }
    /// </code>
    /// </example>
    /// And later used by:
    /// <code>
    /// Foo.Instance.Bar();
    /// </code>
    /// </summary>
    /// <typeparam name="T"></typeparam>
    public class SingletonManager<T> where T : class, new()
    {
        /// <summary>
        /// Returns the running instance of the template
        /// </summary>
        public static T Instance
        {
            // We'll make the instance readonly, since writing
            // makes no sense at all.
            get
            {
                return SingletonCreator.instance;
            }
        }
 
        /// <summary>
        /// A little nested helper class to maintain only 1 running
        /// instance
        /// </summary>
        class SingletonCreator
        {
            /// <summary>
            /// Empty static constructor 
            /// </summary>
            static SingletonCreator()
            {
            }
 
            /// <summary>
            /// Create the new instance
            /// </summary>
            internal static readonly T instance = new T();
        }
    }
}